package org.udr.datamodel;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class Entity implements Serializable {

	private static final long serialVersionUID = 1549379245573336143L;

	private final String name;

	private final Map<String,Attribute> attributes = new HashMap<String,Attribute>();
	
	public Entity (String name) {
		this.name = name;
	}
 
	public String getName() {
		return name;
	}

	public Collection<Attribute> getAttributes() {
		return attributes.values();
	}

	public void addAttribute(Attribute attribute) {
		this.attributes.put(attribute.getName(),attribute);
	}

	/**
	 * Constructs a <code>String</code> with all attributes
	 * in name = value format.
	 *
	 * @return a <code>String</code> representation 
	 * of this object.
	 */
	public String toString()
	{
	    final String TAB = "    ";
	    
	    StringBuffer retValue = new StringBuffer();
	    
	    retValue.append("Entity ( ")
	        .append(super.toString()).append(TAB)
	        .append("name = ").append(this.name).append(TAB)
	        .append(" )");
	    
	    return retValue.toString();
	}

	public Attribute getAttribute(String attributeName ) {
		return attributes.get(attributeName);
	}

	public Entity getAssotiatedEntity(String associationName) {
		Attribute attribute = getAttribute(associationName);
		if (attribute instanceof Association )
			return ((Association)attribute).getTargetEntity();
		else
			return null;
	}

}
